﻿namespace AB.ServiceDirectory.Design.TeamMaintenance
{
    using System;
    using System.Web.UI;
    using BO;
    using Commons.DataFilters;
    using Entities;

    public partial class AddTeam : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                BindDropDownListCountry();

                var filter = new TeamFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Binds the drop down list country.
        /// </summary>
        private void BindDropDownListCountry()
        {
            DropDownListNationCounty.DataSource=TeamBO.SelectContry();
            DropDownListNationCounty.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            var team = GetTeam();

            bool result = TeamBO.AddTeam(team);
            if (result)
            {
                //redirect to teamlists page
                Response.Redirect("~/TeamMaintenance/TeamList.aspx");
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonTypeOfBussiness control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonTypeOfBussiness_Click(object sender, EventArgs e)
        {
            BusinessLookup.Popup();
        }

        /// <summary>
        /// Handles the Click event of the ButtonContactLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonContactLookup_Click(object sender, EventArgs e)
        {
            ContactLookupPopup.Popup();
        }

        /// <summary>
        /// Handles the Click event of the ButtonPostcodeLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonPostcodeLookup_Click(object sender, EventArgs e)
        {
            AddressLookupPopup.Popup();
        }

        /// <summary>
        /// Gets the team from fields in page
        /// </summary>
        /// <returns></returns>
        private Team GetTeam()
        {
            var team = new Team();
            team.TeamName = TextBoxTeamName.Text;
            team.ShortDescription = string.IsNullOrEmpty(TextBoxShortDescription.Text)
                                        ? string.Empty
                                        : TextBoxShortDescription.Text;
            team.ContactId = string.IsNullOrEmpty(HiddenFieldContactId.Value)
                                 ? (int?) null
                                 : int.Parse(HiddenFieldContactId.Value);

            var addressId = GetAddressId();
           
            team.AddressId = addressId;
            team.CityTown = string.IsNullOrEmpty(TextBoxCityTown.Text) ? string.Empty : TextBoxCityTown.Text;
            team.County = string.IsNullOrEmpty(TextBoxCounty.Text) ? string.Empty : TextBoxCounty.Text;
            team.CountryId = DropDownListNationCounty.SelectedValue;
            team.BusinessId = string.IsNullOrEmpty(HiddenFieldBusinessId.Value)
                                  ? (int?) null
                                  : int.Parse(HiddenFieldBusinessId.Value);
            team.FullDescription = string.IsNullOrEmpty(TextBoxFullDescription.Text)
                                       ? string.Empty
                                       : TextBoxFullDescription.Text;
            team.PhoneNumber = string.IsNullOrEmpty(TextBoxPhoneNumber.Text) ? string.Empty : TextBoxPhoneNumber.Text;
            team.Fax = string.IsNullOrEmpty(TextBoxFax.Text) ? string.Empty :TextBoxFax.Text;
            team.Email = string.IsNullOrEmpty(TextBoxEmail.Text) ? string.Empty : TextBoxEmail.Text;
            team.WebAddress = string.IsNullOrEmpty(TextBoxWebAddress.Text) ? string.Empty : TextBoxWebAddress.Text;
            team.IsActive = true;  //hard assign
            team.DepartmentId = 1; //hard assign
            return team;
        }


        /// <summary>
        /// Inserts the new address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        private int InsertNewAddress(Address address)
        {
            return TeamBO.InsertNewAddress(address);
        }

        /// <summary>
        /// Inserts the new town.
        /// </summary>
        /// <param name="town">The town.</param>
        /// <returns></returns>
        private string InsertNewTown(Town town)
        {
            return TeamBO.InsertNewTown(town);
        }

        /// <summary>
        /// Inserts the new county.
        /// </summary>
        /// <param name="county">The county.</param>
        /// <returns></returns>
        private string InsertNewCounty(County county)
        {
            return TeamBO.InsertNewCounty();
        }

        /// <summary>
        /// Gets the address id.
        /// If postcode existed then choose address id from hidden field otherwise choose address id which has just been inserted
        /// </summary>
        /// <returns></returns>
        private int GetAddressId()
        {
            int addressId;
            //check postcode existed or not
            var isexistedAddress = TeamBO.IsExistedPostcode(TextBoxPostcode.Text);
            //if not existed insert new one then return addressId has just inserted
            if (!isexistedAddress)
            {
                var address = new Address();
                address.AddressLine = TextBoxAddressLine1.Text;
                address.AddressLine2 = TextBoxAddressLine2.Text;
                address.AddressLine3 = TextBoxAddressLine3.Text;
                address.Postcode = TextBoxPostcode.Text;
                addressId = InsertNewAddress(address);
            }
            else
            {
                addressId = int.Parse(HiddenFieldAddressId.Value);
            }
            return addressId;
        }

        private string GetTownId()
        {
            string townId;
            //check postcode existed or not
            var isexistedTown = TeamBO.IsExistedTown(TextBoxCityTown.Text);
            //if not existed insert new one then return addressId has just inserted
            if (!isexistedTown)
            {
                var town = new Town();
                town.TownId = TextBoxCityTown.Text;
                town.TownName = TextBoxCityTown.Text;
                town.CountyId = GetCountyId();
                townId = InsertNewTown(town);
            }
            else
            {
                townId = TextBoxCityTown.Text;
            }
            return townId;
        }

        private string GetCountyId()
        {
            string townId;
            //check postcode existed or not
            var isexistedTown = TeamBO.IsExistedTown(TextBoxCityTown.Text);
            //if not existed insert new one then return addressId has just inserted
            if (!isexistedTown)
            {
                var town = new Town();
                town.TownId = TextBoxCityTown.Text;
                town.TownName = TextBoxCityTown.Text;
                town.CountyId = GetCountyId();
                townId = InsertNewTown(town);
            }
            else
            {
                townId = TextBoxCityTown.Text;
            }
            return townId;
        }
    }
}